package com.ep.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ep.common.utils.R;
import com.ep.modules.sys.entity.*;
import com.ep.modules.sys.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 公共控制类
 */
@RestController
@RequestMapping("/sys/common")
public class CommonController extends AbstractController{

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取角色列表
     * @return
     */
    @GetMapping("/roleList")
    public R roleList(){
        List<SysRoleEntity> sysRoleEntityList = sysRoleService.list();
        return R.ok().put("list",sysRoleEntityList);
    }

    /**
     * 获取角色列表
     * @param roleId
     * @return
     */
    @GetMapping("/roleList/{roleId}")
    public R roleList(@PathVariable String roleId){
        //通过角色ID查询所有的该角色用户ID
        LambdaQueryWrapper<SysUserRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRoleEntity::getRoleId,roleId);
        List<SysUserRoleEntity> sysUserRoleEntityList = sysUserRoleService.list(queryWrapper);
        //获取用户信息
        List<SysUserEntity> userList = sysUserRoleEntityList.stream().map((item)->{
            SysUserEntity user = sysUserService.getById(item.getUserId());
            return user;
        }).collect(Collectors.toList());

        return R.ok().put("list",userList);
    }
}
